package br.com.fafica.fast_restaurant.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;

import br.com.fafica.fast_restaurant.model.dao.FabricaConecxao;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TelaEditarProduto extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textFieldNome;
	private JTextField textFieldCod;
	private JTextField textFieldPreco;

	public TelaEditarProduto(java.awt.Frame parent, boolean moda1, String Cod,
			String Preco, String Nome) {

		super(parent, moda1);

		final TelaEditarProduto estaTela = this;
		setBounds(100, 100, 644, 340);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(new Color(0, 102, 0));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		JButton button = new JButton("SALVAR");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				try {

					Connection conn = FabricaConecxao.GeraConexao();
					String query = "update cad_produto set preco = ?, nome = ? where cod = ?";
					PreparedStatement cmd = conn.prepareStatement(query);

					cmd.setString(1, textFieldPreco.getText());
					cmd.setString(2, textFieldNome.getText());
					cmd.setString(3, textFieldCod.getText());

					cmd.executeUpdate();
					cmd.close();

					JOptionPane.showMessageDialog(null,
							"Dados Atualizados com Sucesso");
					estaTela.dispose();

				} catch (SQLException ex) {
					System.out.println("Ocorreu um erro sql" + ex.getMessage());
				}

			}
		});
		JButton button_1 = new JButton("SAIR");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				estaTela.dispose();
			}
		});
		button_1.setOpaque(false);
		textFieldNome = new JTextField();
		textFieldNome.setText(Nome);
		textFieldCod = new JTextField();
		textFieldCod.setEditable(false);
		textFieldCod.setText(Cod);
		JLabel label = new JLabel();
		label.setText("NOME DO PRODUTO:");
		label.setForeground(new Color(255, 215, 0));
		label.setFont(new Font("Tahoma", Font.BOLD, 12));
		label.setBackground(Color.GRAY);
		JLabel label_1 = new JLabel();
		label_1.setText("C\u00D3DIGO DO PRODUTO:");
		label_1.setForeground(new Color(255, 215, 0));
		label_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_1.setBackground(Color.GRAY);
		JLabel label_2 = new JLabel();
		label_2.setText("PRE\u00C7O R$:");
		label_2.setForeground(new Color(255, 215, 0));
		label_2.setFont(new Font("Tahoma", Font.BOLD, 12));
		label_2.setBackground(Color.GRAY);
		textFieldPreco = new JTextField();
		textFieldPreco.setText(Preco);
		JLabel label_3 = new JLabel("CADASTRO CARD\u00C1PIO");
		label_3.setVerticalAlignment(SwingConstants.BOTTOM);
		label_3.setForeground(new Color(255, 215, 0));
		label_3.setFont(new Font("Arial Black", Font.PLAIN, 20));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(44)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(134)
							.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 251, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(textFieldCod, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(textFieldPreco, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(label, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
							.addGap(25)
							.addComponent(textFieldNome, GroupLayout.PREFERRED_SIZE, 371, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(159)
							.addComponent(button, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(54, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(46)
					.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
					.addGap(43)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(textFieldCod, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(textFieldPreco, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(10)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(textFieldNome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(54)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(button, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(34, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
	}

}
